package Game.entity;

import Game.functions.Recyclable;
import Game.utils.CollisionUtils;
import Game.utils.Direction;
import Game.utils.Meta;

public class Bullet extends Element implements Recyclable{

    public Direction bullet_dir;

    public Bullet() {
        super();
    }

    public Bullet(int x, int y) {
        super(x, y);
        super.path = "15-Tankwar\\images\\bullet_u.gif";
        super.getSize();
        super.hp = 1;
    }
/**
 * @param x 坦克的x
 */
public Bullet(int x, int y, int width, int height, Direction d) {
    System.out.println(x + ":" + y + ":" + width + ":" + height);
    //让子弹记录当前坦克的方向
    this.bullet_dir = d;
    //创建子弹对象之前先计算出子弹图片的宽和高
    switch (d) {
        case UP:
            super.path = "15-Tankwar\\images\\bullet_u.gif";
            //先根据坦克方向决定子弹的图片方向 再去计算子弹图片的宽高
            super.getSize();
            super.x = x + (width - super.width) / 2;
            super.y = y - super.height;
            break;
        case DOWN:
            super.path = "15-Tankwar\\images\\bullet_d.gif";
            //先根据坦克方向决定子弹的图片方向 再去计算子弹图片的宽高
            super.getSize();
            super.x = x + (width - super.width) / 2;
            super.y = y + height;
            break;
        case LEFT:
            super.path = "15-Tankwar\\images\\bullet_l.gif";
            //先根据坦克方向决定子弹的图片方向 再去计算子弹图片的宽高
            super.getSize();
            super.x = x - super.width;
            super.y = y + (height - super.height) / 2;
            break;
        case RIGHT:
            super.path = "15-Tankwar\\images\\bullet_r.gif";
            //先根据坦克方向决定子弹的图片方向 再去计算子弹图片的宽高
            super.getSize();
            super.x = x + width;
            super.y = y + (height - super.height) / 2;
            break;
        }
    }

    @Override
    public void draw() {
        super.draw();
        switch (bullet_dir) {
            case UP:
                this.y -= 10;
                break;
            case DOWN:
                this.y += 10;
                break;
            case LEFT:
                this.x -= 10;
                break;
            case RIGHT:
                this.x += 10;
                break;
        }
    }

    /**
     * 03 判断当前子弹是否出界
     * @return
     */
    @Override
    public boolean isOutside() {
        boolean flag = false;
        if (super.x + super.width < 0 || super.y + super.height < 0 || super.x > Meta.WIDTH || super.y > Meta.HEIGHT)
            flag = true;
        return flag;
    }

    /**
     * 04 判断当前子弹是否碰撞到了其他元素
     * @param e
     * @return
     */
    public boolean isCollide(Element e) {
        //获取子弹的宽高坐标
        int x1 = this.x;
        int y1 = this.y;
        int w1 = this.width;
        int h1 = this.height;

        //获取墙体的宽高坐标
        int x2 = e.x;
        int y2 = e.y;
        int w2 = e.width;
        int h2 = e.height;

        return CollisionUtils.isCollisionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);
    }
}